package tanshiru2.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import tanshiru2.config.TsrInfo;
import tanshiru2.entity.Illness;
import tanshiru2.entity.Medicine;
import tanshiru2.entity.User;
import tanshiru2.service.IllnessService;
import tanshiru2.service.MedicineService;
import tanshiru2.util.Assert;
import tanshiru2.util.RespResult;
import tanshiru2.vo.request.FeedBackRequestVo;
import tanshiru2.vo.response.FeedBackResponseVo;
import tanshiru2.vo.response.MyVo;

import javax.servlet.http.HttpSession;

/*
* @Controller
* 表示该类是一个控制器 MVC中的C层
* 可以处理前端浏览器的请求
* */
@Controller
public class IndexController {
    //自动注入配置类
    @Autowired
    private TsrInfo tsrInfo;
    @Autowired
    private IllnessService illnessService;
    @Autowired
    private MedicineService medicineService;

    //表示GET请求/index的URL会到达该方法
    @RequestMapping("/index")
    //参数非必填，内部实际转为null，所以要求类型是引用类型【自定义数据类型、包装类】
    //不可以是基本的数据类型
    //类型
    //如何设置参数的必填项
    //参数名称与前端完全一致？ a-->abc
    //请求参数@RequestParam(name="d",required = true) String ie 表示1前端参数d映射为变量ie，同时要求必填

//    public String index(@RequestParam(name="d",required = true,defaultValue = "设置默认值")String ie, String wd, Integer age){
//        System.out.println(ie);
//        System.out.println("wd="+wd);

    //VO方法
    public String index(Model model, FeedBackRequestVo feedBackRequestVo){
        System.out.println(feedBackRequestVo);
        //业务逻辑代码.....
        //返回结果【数据】
        System.out.println("表示GET请求/index的URL会到达该方法");
        System.out.println("配置信息:"+tsrInfo);
        model.addAttribute("echo",tsrInfo);

        //设置数据回显示界面
        //第一种简单的数据 参数名
        model.addAttribute("username","23软工专升本1班23311145谭诗如");
        //第二种
        FeedBackResponseVo feedBackResponseVo = new FeedBackResponseVo();
        feedBackResponseVo.setName("谭诗如");
        feedBackResponseVo.setEmail("123@qq.com");
        feedBackResponseVo.setTitle("标题");
        feedBackResponseVo.setContent("留言详细内容999");
        model.addAttribute("feedBackInfo",feedBackResponseVo);


        //第三种 Map数据回传
//        HashMap<String ,String> hashMap = new HashMap<>();
//        hashMap.put("name","测试Map陈小燕");
//        hashMap.put("email","123@qq.com");
//        hashMap.put("title","留言标题55");
//        hashMap.put("content","留言详细内容66");
//        model.addAttribute("mapInfo",hashMap);


        //自定义数据类型
        MyVo banannVo = new MyVo();
        banannVo.put("谭诗如","456@qq.com","标题","留言内容");
        model.addAttribute("mapInfo",banannVo);
        return "index";
    }
    @RequestMapping("/findIllness")
    public String findIllness101(Model model,String illnessName,@RequestParam(defaultValue = "1") Integer page){
        //读取illness表的数据，分页[每页显示的行数默认10，当前是第几页]
        IPage<Illness> dataByIllnessName= illnessService.getIllnessPageDataByIllnessName(illnessName, 10, page);
        model.addAttribute("illness",dataByIllnessName.getRecords());
        System.out.println("表示GET请求/index的URL会到达该方法");
        return "search-illness";
    }
//    @RequestMapping("/all-illness")
//    public String allLllness(Model model){
//        return "all-illness";
//    }
    @RequestMapping("/all-medical")
    public String findMedicines(Model model,String medicineName, @RequestParam(defaultValue = "1") Integer page) {
        IPage<Medicine> medicinePageDataByIllnessName = medicineService.getMedicinePageData(medicineName, 5, page);
        model.addAttribute("medicines", medicinePageDataByIllnessName.getRecords());
        return "all-medical";
    }

    @RequestMapping("/findMedicines")
    public String findMedicines(){
        return "illness";
    }
    @RequestMapping("/add-medical")
    public String addMedical(){
        return "add-medical";
    }
    @RequestMapping("/doctor")
    public String doctor(HttpSession session) {
       User loginUser = (User) session.getAttribute("loginUser");
        if (Assert.isEmpty(loginUser)) {
            return "redirect:/index";
        }
        return "doctor";
    }
    @RequestMapping("/feedback.html")
    public String feedback() {
        return "feedback";
    }

    public RespResult reLoginTip() {
        return RespResult.fail("请登录");
    }

    @ModelAttribute
    public void commonRes(Model model){
        model.addAttribute("name","谭诗如");
    }
    //退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }
    @GetMapping("/profile")
    public String profile(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (Assert.isEmpty(loginUser)){
            return "redirect:/index";
        }
        return "profile";
    }
}
